clear; clc; close all
delete *_IRF_* *_dynamic.m *_static.m *.jnl *.log *.eps *.pdf '*.asv' '*.fig' '*.txt' '*.bin' '*.cod' '*.jnl' '*_f.m' '*_ff.m' 'CGH_dplus.dump'... 
       '*_set_auxiliary_variables.m'  'CGH.m' 'CGHdplus' '*.mat'...

%%-----------------------------------------------------------------------------------------------
global fixed_costI normalizationI  setup_ uncertainty_shockI capitalI alpha1I alpha1I_temp oo_ covid_reall_I        

figure3 = 1;
figure4 = 0;
figure5 = 0;

save fig_number figure*


if figure3==1
   uncertainty_shockI        = 1;                        % set to one to have uncertainty shock 
   alpha1I                   = 0;                        % set to one to have a fisrt-moment alpha1 shock
   covid_reall_I             = 0;                        % set to one to have covid shock
end
if figure4==1
   uncertainty_shockI        = 0;                        % set to one to have uncertainty shock 
   alpha1I                   = 1;                        % set to one to have a fisrt-moment alpha1 shock    
   covid_reall_I             = 0;                        % set to one to have covid shock
end
if figure5==1
   uncertainty_shockI        = 1;                        % set to one to have uncertainty shock 
   alpha1I                   = 1;                        % set to one to have a fisrt-moment alpha1 shock    
   covid_reall_I             = 1;                        % set to one to have covid shock
end

fixed_costI               = 1;                          % set to one to have fixed cost of production
normalizationI            = 1;                          % set to one to normalize output (scaling steady-state productivity) 
alpha1I_temp              = 0;

homeprodI                 = 1;                          % SET TO 0 TO GET RID OFF HOME PRODUCTION  (DO NOT CHANGE)
capitalI                  = 1;
home_capitalI             = 0*homeprodI*capitalI;       % SET TO 1 TO HAVE HOME PRODUCTION THAT USES HOME CAPITAL (DO NOT CHANGE)
fixed_homepI              = 1-homeprodI;                % SET TO 1 TO HAVE CONSTANT HOME HOURS AND HOME CAPITAL DYNAMICALLY (DO NOT CHANGE)
kuI                       = 0;                          % SET TO 1 TO HAVE CAPITAL UTILIZATION  (DO NOT CHANGE)
ezI                       = 1;                          % SET TO ONE TO HAVE E-Z PREFERENCES  (DO NOT CHANGE)

loop_modelsI              = 1*homeprodI;  % SET TO ONE IF YOU WANT TO COMPARE MODEL WITH AND WITHOUT HOME PRODUCTION

if covid_reall_I==1
    fixed_homepI              = 1;
end

if alpha1I==1 && uncertainty_shockI==0
    loop_modelsI = 0; % no need to run a loop if you only want a furst-moment alpha1 shock
    alpha1I_temp = 0;
end

if alpha1I==1 && uncertainty_shockI==1
    alpha1I      = 0; % first run the model without the alpha1 shock and then (using alpha1I_temp run it with both uncertainty and alpha1 shock)
    alpha1I_temp = 1;
end
   
save indicators *I
%%-----------------------------------------------------------------------------------------------


%%-----------------------------------------------------------------------------------------------
%% Steady-state and calibration
dynare CGH noclearall nowarn; 


NumberOfParameters = M_.param_nbr;
for ii = 1:NumberOfParameters
  paramname = [deblank(M_.param_names(ii,:))];
  eval([ paramname ' = M_.params(' int2str(ii) ');']);
end

%% Retrieve parameter values implied by calibration
NumberOfEndogenousVariables = M_.orig_endo_nbr; %auxiliary variables are set automatically
for ii = 1:NumberOfEndogenousVariables
  varname = [deblank(M_.endo_names(ii,:)),'_ss'];
  eval([ varname '= oo_.steady_state(' int2str(ii) ');']);
end
%%-----------------------------------------------------------------------------------------------

%%-----------------------------------------------------------------------------------------------
%% Create string for steady-state values implied by calibration
temp = cellstr(M_.endo_names);
NumberOfEndogenousVariables = M_.orig_endo_nbr; %auxiliary variables are set automatically
for ii = 1:NumberOfEndogenousVariables
    temp{ii} = [temp{ii},'_ss'];
end
M_.endo_names_ss = char(temp);
%%-----------------------------------------------------------------------------------------------

% CGH_steadystate_new;

%%-----------------------------------------------------------------------------------------------
param_string = 'empty';
for j=1:length(M_.param_names)
    temp1         = [M_.param_names(j,:),' = ',num2str(M_.params(j,:),15)];
    temp2         = [temp1,';'];
    param_string  = char(param_string,temp2);
end
param_string(1,:) = [];

filenameModel    = 'CGH_dplus';
    
fid  = fopen( [filenameModel,'.mod']);
C    = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);

for j=1:length(M_.param_names)
    line_name              = M_.param_names(j,:);
    line_number            = strmatch(line_name, C{1});
    if size(line_number,1)>1
       line_number = line_number(2);
    end
    line_change            = param_string(j,:);
    C{1}{line_number}      = line_change;
end     



param_string = 'empty';
for j=1:length(M_.endo_names)
    temp1         = [M_.endo_names_ss(j,:),' = ',num2str(oo_.steady_state(j,:),15)];
    temp2         = [temp1,';'];
    param_string  = char(param_string,temp2);
end
param_string(1,:) = [];

for j=1:length(M_.endo_names)
    line_name              = M_.endo_names_ss(j,:);
    line_number            = strmatch(line_name, C{1});
    if size(line_number,1)>1
       line_number = line_number(2);
    end
    line_change            = param_string(j,:);
    C{1}{line_number}      = line_change;
end  


fid_out=fopen( [filenameModel,'.mod'],'wt');
fprintf(fid_out,'%s\n',C{1}{:});
fclose(fid_out);
%%-----------------------------------------------------------------------------------------------

%%-----------------------------------------------------------------------------------------------
%% RUN DYNARE++
setup_.type = 'empty';
orderI           = 4;

if orderI==4
    opt.numSim       = 0;
    opt.deleteAll    = 1;
    opt.orderApp     = orderI;
    out              = RunDynareppPruning_4th(filenameModel,opt);  
end
%%-----------------------------------------------------------------------------------------------


%%
plot_irf              = 0;  % cannot be changed
pruningI              = 0;  % cannot be changed
hpfilterI             = 0;  % cannot be changed
irfI                  = 1;  % cannot be changed
gen_irfI              = 0;  % cannot be changed                             
stoch_ss_irfI         = 1;  % cannot be changed 


[Y_net,C_net,C_m_net,C_n_net,I_m_net,I_n_net,N_m_net,N_n_net,W_net,A_net,Z_net,SigmaA_net,SigmaZ_net,R_net,Pi_net,MU_net,U_m_net,U_n_net,L_net,Alpha1_net,Lambda_net,vxo_vola_IRF_net] = save_irfs(orderI);
save IRF_baseline *_net

if loop_modelsI==1

if covid_reall_I==0
    
   if alpha1I_temp==0 
    fixed_homepI = 1;
    M_.params(strmatch('fixed_homepI',M_.param_names,'exact')) = fixed_homepI;
   else
       alpha1I            = 1;
       M_.params(strmatch('alpha1I',M_.param_names,'exact')) = alpha1I;
   end
else
    
    fixed_homepI = 0;
    M_.params(strmatch('fixed_homepI',M_.param_names,'exact')) = fixed_homepI;
    alpha1I            = 1;
    M_.params(strmatch('alpha1I',M_.param_names,'exact')) = alpha1I;
    
end
    save indicators *I


    if orderI<4
        dynare CGH noclearall nowarn; 
    end

    
    NumberOfParameters = M_.param_nbr;
    for ii = 1:NumberOfParameters
      paramname = [deblank(M_.param_names(ii,:))];
      eval([ paramname ' = M_.params(' int2str(ii) ');']);
    end

    %% Retrieve parameter values implied by calibration
    NumberOfEndogenousVariables = M_.orig_endo_nbr; %auxiliary variables are set automatically
    for ii = 1:NumberOfEndogenousVariables
      varname = [deblank(M_.endo_names(ii,:)),'_ss'];
      eval([ varname '= oo_.steady_state(' int2str(ii) ');']);
    end
    %%-----------------------------------------------------------------------------------------------

    %%-----------------------------------------------------------------------------------------------
    %% Create string for steady-state values implied by calibration
    temp = cellstr(M_.endo_names);
    NumberOfEndogenousVariables = M_.orig_endo_nbr; %auxiliary variables are set automatically
    for ii = 1:NumberOfEndogenousVariables
        temp{ii} = [temp{ii},'_ss'];
    end
    M_.endo_names_ss = char(temp);
    %%-----------------------------------------------------------------------------------------------

    %%-----------------------------------------------------------------------------------------------
    param_string = 'empty';
    for j=1:length(M_.param_names)
        temp1         = [M_.param_names(j,:),' = ',num2str(M_.params(j,:),15)];
        temp2         = [temp1,';'];
        param_string  = char(param_string,temp2);
    end
    param_string(1,:) = [];

    filenameModel    = 'CGH_dplus';
    
    fid  = fopen( [filenameModel,'.mod']);
    C    = textscan(fid, '%s', 'Delimiter', '\n');
    fclose(fid);

    for j=1:length(M_.param_names)
        line_name              = M_.param_names(j,:);
        line_number            = strmatch(line_name, C{1});
        if size(line_number,1)>1
           line_number = line_number(2);
        end
        line_change            = param_string(j,:);
        C{1}{line_number}      = line_change;
    end     

    fid_out=fopen( [filenameModel,'.mod'],'wt');
    fprintf(fid_out,'%s\n',C{1}{:});
    fclose(fid_out);

    if orderI==4
       out              = RunDynareppPruning_4th(filenameModel,opt);  
    end
    
    [Y_net_nohp,C_net_nohp,C_m_net_nohp,C_n_net_nohp,I_m_net_nohp,I_n_net_nohp,N_m_net_nohp,N_n_net_nohp,W_net_nohp,A_net_nohp,Z_net_nohp,SigmaA_net_nohp,SigmaZ_net_nohp,R_net_nohp,Pi_net_nohp,MU_net_nohp,U_m_net_nohp,U_n_net_nohp,L_net_nohp,Alpha1_net_nohp,Lambda_net_nohp,vxo_vola_IRF_net_nohp] = save_irfs(orderI);
    save IRF_no_homeprod *_net   
end


plot_irfs_paper

